package com.wedu.modules.dss;



import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext;

import com.wedu.modules.sys.entity.SysDictEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.util.CellRangeAddressList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义下拉框拦截器.
 */
@Slf4j
public class CustomSheetWriteHandler implements SheetWriteHandler {
//
//    private  Map<String, List<SysDictEntity>> notationMap;
//
//    public CustomSheetWriteHandler(Map<String, List<SysDictEntity>> notationMap) {
//        this.notationMap = notationMap;
//    }

    private   List<String> dictList;
    private Integer total;
    public CustomSheetWriteHandler( List<String> dictList,Integer total) {
        this.dictList = dictList;
        this.total= total;
    }
    @Override// 是 EasyExcel 中 Sheet 写入后的扩展点，用于对已创建的 Sheet 进行自定义操作
    public void afterSheetCreate(SheetWriteHandlerContext context) {

        String[] dict = dictList.toArray(new String[0]);

        for (Integer i = 0; i < total; i++) {
            // 区间设置 第一列第一行和第二行的数据。由于第一行是头，所以第一、二行的数据实际上是第二三行
         //   CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 2, 0, 0);
            //定义下拉列表生效的单元格区域
            CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(2, i+2, 2, 2);
            DataValidationHelper helper = context.getWriteSheetHolder().getSheet().getDataValidationHelper();//从当前 Sheet 中获取数据验证工具类，用于创建验证规则
            DataValidationConstraint constraint = helper.createExplicitListConstraint(dict);//创建下拉选项
            DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);//将下拉选项与目标单元格区域绑定，生成验证规则对象
            context.getWriteSheetHolder().getSheet().addValidationData(dataValidation);//将验证规则应用到 Sheet 中，完成下拉列表设置
        }

    }
}